package com.example.demo.controller;

import com.example.demo.commen.AjaxResult;
import com.example.demo.commen.Constant;
import com.example.demo.commen.SessionUtil;
import com.example.demo.model.Articleinfo;
import com.example.demo.model.Userinfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2023-02-14
 * Time:10:55
 */
@RestController
@RequestMapping("/art")
public class ArticleContoller {
    @Autowired
    private ArticleService articleService;
    @RequestMapping("/mylist")
    public List<Articleinfo> mylist(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if(session!=null&&session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null){
            Userinfo userinfo= (Userinfo) session.getAttribute(Constant.SESSION_USERINFO_KEY);
            return articleService.getMyList(userinfo.getId());
        }
        return null;
    }
    @RequestMapping("/detail")
    public Object detail(Integer aid){
        if(aid!=null&&aid>0){
            return AjaxResult.success(articleService.getDetail(aid));
        }
        return AjaxResult.fail(-1,"查询失败");
    }
    @RequestMapping("/detailbyid")
    public Object detaiById(HttpServletRequest request,Integer aid){
        if(aid!=null&&aid>0){
            Articleinfo articleinfo=articleService.getDetail(aid);
            Userinfo userinfo= SessionUtil.getLoginUser(request);
            if(userinfo!=null&&articleinfo!=null&&
            articleinfo.getUid()== userinfo.getId()){
                return AjaxResult.success(articleService.getDetail(aid));
            }
        }
        return AjaxResult.fail(-1,"查询失败");
    }
    @RequestMapping("/update")
    public int update(HttpServletRequest request,Integer aid,String title,String content){
        //非空校验
        if(aid!=null&&title!=null&&content!=null&&aid>0){
            if(aid>0&&!"".equals(title)&&!"".equals(content)) {
                Userinfo userinfo=SessionUtil.getLoginUser(request);
                if(userinfo!=null&&userinfo.getId()>0){
                    return articleService.update(aid,userinfo.getId(),title,content);
                }
            }
        }
        return 0;
    }
    @RequestMapping("/list")
    public List<Articleinfo> getList(Integer pindex,Integer psize){
        if(pindex==null||psize==null){
            return null;
        }
        int offset=(pindex-1)*psize;
        return articleService.getList(psize,offset);
    }
    @RequestMapping("/totalpage")
    public Integer total(Integer psize){
        if(psize!=null){
            int total=articleService.total();
            int totalpage= (int) Math.ceil(total*1.0/psize);
            return totalpage;
        }
        return null;
//        int total=articleService.total();
//        int page=0;
//        if(total%psize>0){
//            return total/psize+1;
//        }else{
//            return total/psize;
//        }
    }
    @RequestMapping("/add")
    public Integer add(HttpServletRequest request,String title,String content){
        //非空校验
        if(title!=null&&content!=null){
            if(!"".equals(title)&&!"".equals(content)) {
                Userinfo userinfo=SessionUtil.getLoginUser(request);
                if(userinfo!=null&&userinfo.getId()>0){
                    return articleService.add(title,content,userinfo.getId());
                }
            }
        }
        return null;
    }
    @RequestMapping("/mydel")
    public Integer mydel(HttpServletRequest request,Integer id){
        HttpSession session= request.getSession(false);
        if(session!=null&&session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null){
            Userinfo userinfo=(Userinfo) session.getAttribute(Constant.SESSION_USERINFO_KEY);
            //判断这篇文章是否属于
            //得到这篇文章的uid
            if(id>0){
                if(articleService.selectUid(id).equals(userinfo.getId())){
                    return articleService.delete(id);
                }
            }
        }
        return null;
    }


}
